
import database.DBClass;
import static database.DBClass.createConnection;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.UserBean;
import validation.LoginValidation;

public class LoginServlet extends HttpServlet {

    DBClass dbobject = new DBClass();
    UserBean bean = new UserBean();
    String otoritas;

    public LoginServlet() {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //Here username and password are the names which I have given in the input box in Login.jsp page. Here I am retrieving the values entered by the user and keeping in instance variables for further use.
            HttpSession session = request.getSession();
            
            String userName = request.getParameter("username");
            String password = request.getParameter("password");
            session.setAttribute("user", userName);
//           System.out.println("user1"+userName);
            bean.setUserName(userName);
            bean.setPassword(password);
            System.out.println("useruser"+userName);
            
//
            Connection con = createConnection();
            PreparedStatement pstmt = con.prepareStatement("select otoritas from userdetails where userName='" + userName + "'");
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {

                otoritas = rs.getString(1);
                System.out.println("otoritas: " + otoritas);

            }


            UserBean loginBean = new UserBean(); //creating object for LoginBean class, which is a normal java class, contains just setters and getters. Bean classes are efficiently used in java to access user information wherever required in the application.

            loginBean.setUserName(userName); //setting the username and password through the loginBean object then only you can get it in future.
            loginBean.setPassword(password);

            LoginValidation loginValid = new LoginValidation(); //creating object for LoginValidation. This class contains main logic of the application.

            String userValidate = loginValid.authenticateUser(loginBean); //Calling authenticateUser function

            if (userValidate.equals("SUCCESS")) //If function returns success string then user will be rooted to Home page
            {
                session.setAttribute("userName", userName);
                if (otoritas.equals("1")) {
                    request.setAttribute("userName", userName); //with setAttribute() you can define a "key" and value pair so that you can get it in future using getAttribute("key")
                    List list = dbobject.getAlldetails();
                    request.setAttribute("list", list);
                    RequestDispatcher rd = request.getRequestDispatcher("home.jsp");
                    rd.forward(request, response);
                } else if (otoritas.equals("0")) {
                    request.setAttribute("userName", userName);
                    List list = dbobject.getAlldetails();
                    request.setAttribute("list", list);
                    RequestDispatcher rd = request.getRequestDispatcher("index_1.jsp");

                    rd.forward(request, response);
                }

            } else {
                request.setAttribute("errMessage", userValidate); //If authenticateUser() function returnsother than SUCCESS string it will be sent to Login page again. Here the error message returned from function has been stored in a errMessage key.
                request.getRequestDispatcher("login.jsp").forward(request, response);//forwarding the request
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
